package com.prototipo_picshare;

import java.lang.annotation.Retention;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class Unirse_a_grupo extends Activity implements OnItemClickListener,OnClickListener{
	Button buscar_dispocitivo;
	ListView lv;
	ArrayAdapter<String> lista;//listAdapter
	BluetoothAdapter Bluetooth_adaptador;
	Set<BluetoothDevice> lista_de_dispocitivos;
	ArrayList<String> dispocitivos_en_paridad;
	IntentFilter filtro=new IntentFilter(BluetoothDevice.ACTION_FOUND);
	BroadcastReceiver reciver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_unirse_a_grupo);
		//botn buscar dispocitivos
		buscar_dispocitivo= (Button) findViewById(R.id.BTbuscar_dispocitivo);
		buscar_dispocitivo.setOnClickListener(this);
		
		
		
	}

	private void init() {
		
			
				
				//llenar lista
				lv = (ListView) findViewById(R.id.listView1);
				lv.setOnItemClickListener(this);
				lista= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
				lv.setAdapter(lista);
				
				
				Bluetooth_adaptador =BluetoothAdapter.getDefaultAdapter();
				dispocitivos_en_paridad=new ArrayList<String>();
				
				reciver= new  BroadcastReceiver() {
					
					@Override
					public void onReceive(Context context, Intent intent) {
						// TODO Auto-generated method stub
						String action =intent.getAction();
						if(BluetoothDevice.ACTION_FOUND.equals(action)){
							
							BluetoothDevice dispocitivo = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
							lista.add(dispocitivo.getName()+"\n"+dispocitivo.getAddress());
						
						}else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
							
							
						}else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
						
							
						}else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
							if (Bluetooth_adaptador.getState()==BluetoothAdapter.STATE_OFF) {
								encender_bluetoorh();
							}
							
						}
						
					}
				};
				
				registerReceiver(reciver, filtro);
				filtro= new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
				registerReceiver(reciver, filtro);
				filtro= new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
				registerReceiver(reciver, filtro);
				filtro= new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
				registerReceiver(reciver, filtro);
				
				
				if(Bluetooth_adaptador==null){
					Toast.makeText(this,"no se detecto bluetooth", 0).show();
					finish();
				}else{
					if(!Bluetooth_adaptador.isEnabled()){
							encender_bluetoorh();
						//Intent intento_bluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
						//startActivityForResult(intento_bluetooth,1);
					}
					getDispocitivos();
					StartDiscovery();
				}
				
	}

	
	private void StartDiscovery() {
		Bluetooth_adaptador.cancelDiscovery();
		Bluetooth_adaptador.startDiscovery();
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(reciver);
	}
	
	private void encender_bluetoorh() {
		Intent intento_bluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		startActivityForResult(intento_bluetooth,1);
	}

	private void getDispocitivos() {
		lista_de_dispocitivos= Bluetooth_adaptador.getBondedDevices();
		if (lista_de_dispocitivos.size()>0) {
			for(BluetoothDevice device:lista_de_dispocitivos){
				dispocitivos_en_paridad.add(device.getName());
				lista.add(device.getName()+"\n"+device.getAddress());
				
				
			}
		}
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==RESULT_CANCELED){
			Toast.makeText(getApplicationContext(),"el bluetooth debe de ser activado para continuar", Toast.LENGTH_SHORT).show();
			finish();
		}
	}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.unirse_a_grupo, menu);
		return true;
	}

	

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
		Intent it_PE = new Intent(this, Pantalla_esoera.class );
		startActivity(it_PE);	
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) 
		{
			case R.id.BTbuscar_dispocitivo:
				init();
			break;
			

			default:
		break;
		
		}
		
	}

}
